package com.cysion.study.adapter

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.study.R
import com.cysion.study.entity.FamilyeduAppointEntity
import com.cysion.study.ktbox.base.BaseAdapter
import com.cysion.study.ktbox.base.BaseViewHolder
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import kotlinx.android.synthetic.main.item_teacher_p.view.*

class PTeacherFragmentAdapter(mEntities: MutableList<FamilyeduAppointEntity>, mContext: Context) :
    BaseAdapter<FamilyeduAppointEntity>(mEntities, mContext) {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder<FamilyeduAppointEntity> {

        return ProMoneyHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_teacher_p, parent, false)
        )
    }

    class ProMoneyHolder(itemView: View) : BaseViewHolder<FamilyeduAppointEntity>(itemView) {
        override fun fillData(obj: FamilyeduAppointEntity, position: Int) {
            itemView?.apply {
                itemView.tv_name.text = obj.stuNickName
                Glide.with(context).load(obj.stuHeadImage).apply(
                    RequestOptions().placeholder(R.mipmap.default_head)
                ).into(itemView.iv_head)
                itemView.tv_num.text=obj.canceledNum+"/"+obj.totalNum
                itemView.tv_phone.text=obj.familyLink
                itemView.tv_address.text=obj.supplement+" "+obj.address
                itemView.setOnClickListener {
                    mOnTypeClickListener?.invoke(obj, position, ITEM_CLICK)
                }
                itemView.tv_phone.setOnClickListener(View.OnClickListener {
                    if (TextUtils.isEmpty(obj.phoneReal)) {
                        ShowToast("系统未返回真实号码")
                        return@OnClickListener
                    }
                    //拨打电话
                    val intent = Intent()
                    intent.action = Intent.ACTION_DIAL//dial是拨号的意思
                    intent.data = Uri.parse("tel:" + obj.phoneReal)//这个tel不能改，后面的数字可以随便改
                    context.startActivity(intent)
                })
                itemView.tv_address.setOnClickListener(View.OnClickListener {
                    if (TextUtils.isEmpty(obj.latitude) || TextUtils.isEmpty(obj.supplement)) {
                        ShowToast("未返回地址信息 不能导航")
                        return@OnClickListener
                    }
                    UIHelper.goToBaidu(context, obj!!.latitude, obj!!.longitude,
                        obj!!.supplement + obj!!.address
                    )
                })
            }

        }

    }

}



